package com.qfedu.fmmall.service.impl;

import com.qfedu.fmmall.common.status.ResStatus;
import com.qfedu.fmmall.common.vo.ResultVO;
import com.qfedu.fmmall.dao.UserAddrMapper;
import com.qfedu.fmmall.entity.UserAddr;
import com.qfedu.fmmall.service.UserAddrService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : yanBin
 * @date :2021/12/20 15:11
 */
@Service
public class UserAddrServiceImpl implements UserAddrService {
    @Resource
    UserAddrMapper userAddrMapper;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public ResultVO listAddrByUId(int userId) {
        Example example = new Example(UserAddr.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",userId);
        criteria.andEqualTo("status",1);
        List<UserAddr> userAddrs = userAddrMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(userAddrs)){
            return new ResultVO(ResStatus.NO.ordinal(), "没有默认收件地址",null);
        }
        return new ResultVO(ResStatus.OK.ordinal(), "查询默认地址成功",userAddrs.get(0));

    }
}
